#include "VSVertexFormat.h"
#include "VSVertexBuffer.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSVertexFormat,VSBind)
static bool gs_bStreamRegistered_VSVertexFormat = VSVertexFormat::RegisterMainFactory();
bool VSVertexFormat::ms_bRegisterMainFactory = false;
/*VSPointer<VSVertexFormat> VSVertexFormat::DefaultVF[VF_MAX];*/
bool VSVertexFormat::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 
		VSMain::AddInitialFuntion(VSVertexFormat::InitialClassFactory); 
// 		VSMain::AddInitialFuntion(VSVertexFormat::InitialDefaultState);
// 		VSMain::AddTerminalFuntion(VSVertexFormat::TerminalDefaultState);
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
// bool VSVertexFormat::InitialDefaultState()
// {
// 	VSVertexFormat * p = NULL;
// 	for(unsigned int i = 0 ; i< VF_MAX ; i++)
// 	{
// 		p = NULL;
// 		p = VS_NEW VSVertexFormat();;
// 		
// 		if(p)
// 		{
// 			p->m_iVertexFormatCode = i;
// 			VSResourceManager::GetVertexFormatSet().AddResource(i,p,1);
// 			DefaultVF[i] = p;
// 			
// 		}
// 		else
// 			return 0;
// 	}
// 	return 1;
// }
// bool VSVertexFormat::TerminalDefaultState()
// {
// 
// 	for(unsigned int i = 0 ; i< VF_MAX ; i++)
// 	{
// 
// 		DefaultVF[i] = NULL;
// 
// 	}
// 	return 1;
// }
bool VSVertexFormat::InitialClassFactory()
{
	ms_ClassFactory.AddElement(ms_Type.GetName(),FactoryFunc);
	
	return 1;
}
VSObject * VSVertexFormat::FactoryFunc()
{
	return VS_NEW VSVertexFormat;

}
VSVertexFormat::~VSVertexFormat()
{
	ReleaseResource();
 	
}
VSVertexFormat::VSVertexFormat()
{
	m_bIsStatic = 1;

}

unsigned int VSVertexFormat::DiskUse()const
{
	unsigned int Use = VSBind::DiskUse();
	Use += sizeof(unsigned int);

	Use += sizeof(unsigned int);
	Use += sizeof(VSVertexFormat::VERTEXFORMAT_TYPE) * m_FormatArray.GetNum();
	return Use;

}
bool VSVertexFormat::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSBind::Save(rStream,iSaveUse))
		return false;


	if(!rStream.Write(&m_uiVertexFormatCode,sizeof(int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	unsigned int uiFormatNum = m_FormatArray.GetNum();
	if(!rStream.Write(&uiFormatNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	for (unsigned int i = 0 ; i < uiFormatNum ; i++)
	{
		VSVertexFormat::VERTEXFORMAT_TYPE Type = m_FormatArray[i];
		if(!rStream.Write(&Type,sizeof(VSVertexFormat::VERTEXFORMAT_TYPE)))
			return 0;
		iSaveUse += sizeof(VSVertexFormat::VERTEXFORMAT_TYPE);
	}
	return 1;
}
bool VSVertexFormat::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!VSBind::Load(rStream,iSaveUse))
		return false;

	if(!rStream.Read(&m_uiVertexFormatCode,sizeof(int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	

	unsigned int uiFormatNum = 0;
	if(!rStream.Read(&uiFormatNum,sizeof(unsigned int)))
		return 0;
	iSaveUse += sizeof(unsigned int);

	m_FormatArray.SetBufferNum(uiFormatNum);
	for (unsigned int i = 0 ; i < uiFormatNum ; i++)
	{
		VSVertexFormat::VERTEXFORMAT_TYPE Type;
		if(!rStream.Read(&Type,sizeof(VSVertexFormat::VERTEXFORMAT_TYPE)))
			return 0;
		iSaveUse += sizeof(VSVertexFormat::VERTEXFORMAT_TYPE);
		m_FormatArray.AddElement(Type);
	}
	return 1;
}
bool VSVertexFormat::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSVertexFormat* Temp = DynamicCast<VSVertexFormat>(pObject); 
	if(!Temp)
		return 0;
	if(VSBind::Clone(pObject,uiType))
	{
		m_uiVertexFormatCode = Temp->m_uiVertexFormatCode;
		m_FormatArray = Temp->m_FormatArray;
		return 1;
	}
	else
	{
		return 0;

	}
}


bool VSVertexFormat::RegisterSelf(VSStream & rStream)const
{
// 	for (unsigned int i = 0 ; i < VF_MAX ; i++)
// 	{
// 		if(this == DefaultVF[i])
// 			return 1;
// 	}
	return VSBind::RegisterSelf(rStream);
}
// bool VSVertexFormat::LoadAllDefault(VSRenderer *pRender)
// {
// 
// 	for(unsigned int i = 0 ; i < VSVertexFormat::VF_MAX ; i++)
// 		if(!VSVertexFormat::DefaultVF[i]->LoadResource(pRender))
// 			return 0;
// 	return 1;
// }
// bool VSVertexFormat::ReleaseAllDefault()
// {
// 	for (unsigned int i = 0 ; i < VF_MAX ; i++)
// 	{
// 		if(!DefaultVF[i]->ReleaseResource())
// 			return 0;
// 	}
// 	return 1;
// }
bool VSVertexFormat::OnLoadResource(VSResourceIdentifier *&pID)
{
	if(!m_pUser)
		return 0;

	if(!m_pUser->OnLoadVBufferFormat (this,pID))
		return 0;

	return 1;
}
bool VSVertexFormat::OnReleaseResource(VSResourceIdentifier *pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnReleaseVBufferFormat(pID))
		return 0;
	return 1;
}
